<?php

namespace Frieza\Modules\IdeaBook\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\service\search\IdeaBookSearch;
use Doraemon\tools\HhzNiceLivingUrl;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Doraemon\tools\ImgUrl;
use Doraemon\model\Member;

/**
 * 收藏夹搜索
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     zhouyang  <zhouyang@haohaozhu.com>
 * @license    GPL http://www.haohaozhu.com
 * @link       http://www.haohaozhu.com
 */

class SearchController extends Controller
{
    const PAGE_SIZE = 20;

    public function __construct()
    {
        parent::__construct();
        Admin::script('$(".pjax-container").attr("id","pjax-container");');
    }

    public function index(Request $request, Content $content)
    {
        $request_data = $request->all();
        $page_size = $request->input("per_page", self::PAGE_SIZE);
        $page = $request->input("page", 1);

        $filter = $lists = [];
        $total = 0;
        if (!empty($request_data['title'])) {
            $filter['title'] = $request_data['title'];
        }

        if (!empty($request_data['uid'])) {
            $filter['uid'] = $request_data['uid'];
        }

        $data = IdeaBookSearch::getIdeaBookListByFilter($filter, $page, $page_size);
        $lists = $data['rows'];
        $total = $data['total'];

        if (!empty($lists)) {
            $user_ids = array_column($lists, 'uid');
            $member_info = Member::getUsers($user_ids);

            foreach ($lists as &$item) {
                $params = [
                    'link' => HhzNiceLivingUrl::getIdeaBookLink($item['idea_book_id']),
                    'type' => 'link',
                ];
                $item['qr_code'] = ImgUrl::getQrcode(json_encode($params));
                $item['nick'] = empty($member_info[$item['uid']]['nick']) ? '' : $member_info[$item['uid']]['nick'];
            }
            unset($item);
        }

        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);

        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel) {
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();
                $grid->disableCreateButton();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->equal('title', '收藏夹名称')->placeholder('请输入收藏夹名称');
                    $filter->equal('uid', '用户UID')->placeholder('请输入用户uid');
                    $filter->expand();
                });

                $grid->column('title', '收藏夹名称');
                $grid->column('idea_book_id', '收藏夹ID');
                $grid->column('uid', '用户UID');
                $grid->column('nick', '用户昵称');
                $grid->column("qr_code", '二维码')->image('', 180, 180);
                $grid->actions(function (Grid\Displayers\Actions $actions) {
                    $actions->disableDelete();
                    $actions->disableEdit();
                });
            }
        );

        $tab = new Tab();
        $tab->add('收藏夹搜索', $gridList->render());
        $content->row($tab);
        return $content
            ->header('收藏夹管理')
            ->description(' ')
            ->breadcrumb(['text' => '收藏夹搜索']);
    }
}
